class SceneIntro extends AbstractScene

	frames 					: null
	currentFrame			: 0

	scale 					: null
	offsetX 				: 0
	offsetY 				: 0
	color 					: null

	boids 					: null
	numBoids				: 0


	constructor: (@ctx, @index) ->
		super

		@scale = 1
		@offsetY = if (@frames[@currentFrame][0].y < 0) then 50 else - 50
		@color = 'rgba(75, 200, 230, 0.4)'

		@initBoids()


	update: ->
		if (!@audio.startedAt) then return
		if (@audio.paused) then return

		# dance!
		dt = Date.now() - @audio.startedAt
		frame = 0
		if (dt > 2660) then frame = 1
		if (dt > 4820) then frame = 2
		if (dt > 6980) then frame = 3
		if (dt > 9140) 
			app.view.scenes.killIntro()
			return

		@offsetY = if (@frames[frame][0].y < 0) then 50 else - 50

		# if (frame != @currentFrame) then @initBoids(frame)
		@currentFrame = frame


	draw: ->
		hw = @ctx.width * .5
		hh = @ctx.height * .5

		@ctx.fillStyle = '#cccccc'
		@ctx.beginPath()

		frame = @frames[@currentFrame]
		for i in [0...frame.length]
			a = frame[i]
			@ctx.lineTo(a.x * @scale + hw + @offsetX, a.y * @scale + hh + @offsetY)

		@ctx.fill()
		@ctx.closePath()

		i = 0
		for boid in @boids
			a = frame[i]
			boid.acc.addSelf(boid.arrive(new Vec3D(boid.pos.x, @ctx.height, 0)).scaleSelf(0.2))
			boid.acc.addSelf(boid.wander().scaleSelf(0.2))
			boid.update()
			boid.bounce()
			i++

		for boid in @boids
			boid.draw()

		###
		@ctx.strokeStyle = @color
		@ctx.fillStyle = @color
		@ctx.beginPath()

		for b in @boids
			@ctx.lineTo(b.pos.x, b.pos.y)

		# @ctx.fill()
		@ctx.stroke()
		@ctx.closePath()
		###



	initReindeer: ->
		@frames = []
		@frames[0] = [ {x:20, y:200}, {x:6, y:192}, {x:-9, y:180}, {x:-17, y:167}, {x:-24, y:154}, {x:-26, y:140}, {x:-23, y:128}, {x:-17, y:119}, {x:-17, y:103}, {x:-17, y:87}, {x:-20, y:77}, {x:-27, y:67}, {x:-43, y:63}, {x:-59, y:59}, {x:-69, y:47}, {x:-75, y:33}, {x:-81, y:20}, {x:-93, y:10}, {x:-111, y:2}, {x:-128, y:-5}, {x:-142, y:-20}, {x:-159, y:-42}, {x:-165, y:-61}, {x:-171, y:-74}, {x:-184, y:-86}, {x:-192, y:-102}, {x:-197, y:-123}, {x:-200, y:-141}, {x:-202, y:-164}, {x:-193, y:-150}, {x:-190, y:-138}, {x:-184, y:-124}, {x:-176, y:-105}, {x:-174, y:-96}, {x:-164, y:-91}, {x:-155, y:-94}, {x:-150, y:-102}, {x:-146, y:-118}, {x:-144, y:-133}, {x:-141, y:-148}, {x:-136, y:-130}, {x:-136, y:-117}, {x:-138, y:-97}, {x:-142, y:-82}, {x:-142, y:-65}, {x:-136, y:-49}, {x:-125, y:-33}, {x:-113, y:-24}, {x:-96, y:-17}, {x:-85, y:-11}, {x:-69, y:-4}, {x:-60, y:2}, {x:-44, y:5}, {x:-35, y:9}, {x:-20, y:9}, {x:-14, y:-4}, {x:-9, y:-23}, {x:-5, y:-6}, {x:-3, y:11}, {x:3, y:22}, {x:15, y:23}, {x:27, y:20}, {x:35, y:7}, {x:44, y:-29}, {x:48, y:-13}, {x:53, y:0}, {x:62, y:0}, {x:73, y:-5}, {x:80, y:-17}, {x:86, y:-30}, {x:94, y:-35}, {x:107, y:-37}, {x:122, y:-42}, {x:133, y:-55}, {x:144, y:-72}, {x:151, y:-93}, {x:153, y:-111}, {x:150, y:-129}, {x:139, y:-140}, {x:126, y:-157}, {x:121, y:-173}, {x:119, y:-190}, {x:130, y:-171}, {x:138, y:-159}, {x:144, y:-153}, {x:150, y:-164}, {x:152, y:-179}, {x:158, y:-167}, {x:160, y:-157}, {x:165, y:-147}, {x:175, y:-143}, {x:186, y:-153}, {x:190, y:-162}, {x:191, y:-177}, {x:186, y:-200}, {x:196, y:-190}, {x:200, y:-176}, {x:202, y:-162}, {x:197, y:-146}, {x:189, y:-133}, {x:180, y:-114}, {x:175, y:-94}, {x:168, y:-78}, {x:163, y:-66}, {x:157, y:-57}, {x:144, y:-45}, {x:153, y:-48}, {x:163, y:-56}, {x:172, y:-60}, {x:172, y:-50}, {x:161, y:-38}, {x:148, y:-26}, {x:126, y:-21}, {x:113, y:-13}, {x:100, y:-6}, {x:93, y:4}, {x:89, y:21}, {x:84, y:32}, {x:74, y:40}, {x:65, y:50}, {x:60, y:56}, {x:56, y:66}, {x:59, y:75}, {x:62, y:82}, {x:66, y:92}, {x:69, y:103}, {x:68, y:114}, {x:66, y:121}, {x:68, y:129}, {x:70, y:138}, {x:73, y:156}, {x:74, y:168}, {x:74, y:176}, {x:73, y:187}, {x:65, y:195}, {x:54, y:199}, {x:40, y:201}, {x:31, y:201} ]
		@frames[1] = [ {x:20, y:200}, {x:6, y:192}, {x:-9, y:180}, {x:-17, y:167}, {x:-24, y:154}, {x:-26, y:140}, {x:-23, y:128}, {x:-17, y:119}, {x:-13, y:102}, {x:-14, y:83}, {x:-16, y:70}, {x:-26, y:57}, {x:-38, y:46}, {x:-52, y:37}, {x:-66, y:28}, {x:-79, y:24}, {x:-90, y:21}, {x:-103, y:16}, {x:-119, y:3}, {x:-131, y:-7}, {x:-147, y:-21}, {x:-163, y:-29}, {x:-182, y:-36}, {x:-205, y:-42}, {x:-220, y:-43}, {x:-242, y:-48}, {x:-232, y:-54}, {x:-216, y:-55}, {x:-201, y:-54}, {x:-195, y:-63}, {x:-199, y:-75}, {x:-202, y:-87}, {x:-210, y:-102}, {x:-196, y:-90}, {x:-188, y:-77}, {x:-182, y:-63}, {x:-172, y:-50}, {x:-158, y:-43}, {x:-146, y:-44}, {x:-141, y:-57}, {x:-145, y:-69}, {x:-149, y:-80}, {x:-138, y:-73}, {x:-131, y:-59}, {x:-127, y:-49}, {x:-124, y:-34}, {x:-120, y:-24}, {x:-110, y:-13}, {x:-96, y:-2}, {x:-82, y:4}, {x:-69, y:11}, {x:-51, y:16}, {x:-36, y:21}, {x:-21, y:24}, {x:-21, y:13}, {x:-21, y:7}, {x:-13, y:9}, {x:-6, y:14}, {x:-2, y:18}, {x:6, y:24}, {x:16, y:25}, {x:25, y:19}, {x:23, y:9}, {x:16, y:-4}, {x:6, y:-18}, {x:-9, y:-35}, {x:-25, y:-52}, {x:-39, y:-66}, {x:-54, y:-79}, {x:-76, y:-92}, {x:-97, y:-107}, {x:-112, y:-116}, {x:-129, y:-124}, {x:-146, y:-140}, {x:-123, y:-134}, {x:-105, y:-125}, {x:-79, y:-114}, {x:-66, y:-106}, {x:-51, y:-100}, {x:-48, y:-110}, {x:-49, y:-121}, {x:-52, y:-133}, {x:-58, y:-156}, {x:-47, y:-142}, {x:-41, y:-129}, {x:-38, y:-116}, {x:-33, y:-99}, {x:-30, y:-85}, {x:-19, y:-66}, {x:-5, y:-48}, {x:8, y:-38}, {x:22, y:-38}, {x:35, y:-43}, {x:44, y:-51}, {x:51, y:-67}, {x:54, y:-48}, {x:44, y:-34}, {x:30, y:-22}, {x:32, y:-11}, {x:40, y:-1}, {x:51, y:2}, {x:67, y:0}, {x:80, y:-8}, {x:84, y:-18}, {x:90, y:-14}, {x:100, y:-21}, {x:102, y:-18}, {x:103, y:-13}, {x:101, y:-8}, {x:94, y:-1}, {x:88, y:2}, {x:80, y:6}, {x:72, y:11}, {x:64, y:14}, {x:58, y:23}, {x:59, y:34}, {x:67, y:41}, {x:83, y:48}, {x:94, y:52}, {x:110, y:57}, {x:125, y:61}, {x:141, y:66}, {x:152, y:71}, {x:157, y:78}, {x:157, y:88}, {x:153, y:96}, {x:143, y:103}, {x:124, y:110}, {x:112, y:121}, {x:103, y:131}, {x:90, y:150}, {x:86, y:164}, {x:82, y:177}, {x:76, y:188}, {x:65, y:195}, {x:54, y:199}, {x:40, y:201}, {x:31, y:201} ]
		@frames[2] = [ {x:-74, y:-156}, {x:-70, y:-137}, {x:-68, y:-121}, {x:-69, y:-109}, {x:-70, y:-96}, {x:-75, y:-84}, {x:-85, y:-73}, {x:-94, y:-63}, {x:-103, y:-52}, {x:-110, y:-39}, {x:-117, y:-25}, {x:-121, y:-13}, {x:-126, y:-2}, {x:-134, y:11}, {x:-143, y:26}, {x:-147, y:37}, {x:-153, y:46}, {x:-154, y:54}, {x:-163, y:63}, {x:-171, y:67}, {x:-179, y:72}, {x:-185, y:80}, {x:-185, y:87}, {x:-188, y:96}, {x:-195, y:104}, {x:-203, y:111}, {x:-211, y:124}, {x:-212, y:135}, {x:-209, y:146}, {x:-204, y:154}, {x:-196, y:157}, {x:-187, y:156}, {x:-178, y:152}, {x:-169, y:144}, {x:-162, y:132}, {x:-158, y:119}, {x:-154, y:109}, {x:-145, y:101}, {x:-140, y:90}, {x:-132, y:84}, {x:-121, y:86}, {x:-110, y:89}, {x:-99, y:91}, {x:-92, y:89}, {x:-87, y:74}, {x:-80, y:60}, {x:-72, y:43}, {x:-59, y:25}, {x:-48, y:6}, {x:-34, y:-17}, {x:-22, y:-40}, {x:-14, y:-59}, {x:-8, y:-75}, {x:-3, y:-92}, {x:-2, y:-118}, {x:-3, y:-140}, {x:-3, y:-156}, {x:40, y:-156}, {x:47, y:-147}, {x:57, y:-135}, {x:63, y:-122}, {x:65, y:-106}, {x:68, y:-82}, {x:69, y:-61}, {x:72, y:-42}, {x:71, y:-22}, {x:75, y:-11}, {x:75, y:2}, {x:75, y:18}, {x:76, y:36}, {x:78, y:52}, {x:77, y:64}, {x:83, y:68}, {x:95, y:69}, {x:95, y:82}, {x:95, y:98}, {x:96, y:109}, {x:99, y:115}, {x:107, y:115}, {x:124, y:116}, {x:138, y:116}, {x:154, y:119}, {x:172, y:122}, {x:188, y:121}, {x:198, y:118}, {x:209, y:113}, {x:211, y:104}, {x:205, y:98}, {x:198, y:99}, {x:185, y:99}, {x:175, y:98}, {x:166, y:91}, {x:157, y:77}, {x:149, y:65}, {x:141, y:55}, {x:136, y:34}, {x:133, y:17}, {x:128, y:-3}, {x:129, y:-38}, {x:129, y:-67}, {x:130, y:-93}, {x:127, y:-118}, {x:124, y:-144}, {x:114, y:-177}, {x:40, y:-156}, {x:-3, y:-156}, {x:-3, y:-191} ] 
		@frames[3] = [ {x:-150, y:-156}, {x:-143, y:-140}, {x:-139, y:-126}, {x:-137, y:-113}, {x:-139, y:-103}, {x:-144, y:-90}, {x:-153, y:-78}, {x:-161, y:-66}, {x:-170, y:-53}, {x:-179, y:-39}, {x:-188, y:-22}, {x:-197, y:-5}, {x:-205, y:11}, {x:-213, y:23}, {x:-221, y:35}, {x:-227, y:47}, {x:-229, y:59}, {x:-232, y:66}, {x:-237, y:77}, {x:-241, y:82}, {x:-250, y:89}, {x:-252, y:99}, {x:-251, y:107}, {x:-251, y:115}, {x:-250, y:122}, {x:-240, y:132}, {x:-230, y:140}, {x:-219, y:148}, {x:-214, y:153}, {x:-203, y:158}, {x:-193, y:160}, {x:-182, y:160}, {x:-172, y:156}, {x:-168, y:148}, {x:-170, y:136}, {x:-176, y:127}, {x:-187, y:118}, {x:-191, y:110}, {x:-186, y:97}, {x:-174, y:101}, {x:-166, y:89}, {x:-156, y:73}, {x:-149, y:64}, {x:-143, y:54}, {x:-136, y:42}, {x:-128, y:29}, {x:-120, y:15}, {x:-111, y:1}, {x:-100, y:-16}, {x:-90, y:-30}, {x:-79, y:-48}, {x:-69, y:-67}, {x:-66, y:-85}, {x:-66, y:-102}, {x:-66, y:-122}, {x:-66, y:-139}, {x:-70, y:-156}, {x:-5, y:-160}, {x:17, y:-139}, {x:31, y:-128}, {x:39, y:-117}, {x:47, y:-106}, {x:53, y:-86}, {x:59, y:-65}, {x:65, y:-46}, {x:71, y:-22}, {x:75, y:-11}, {x:79, y:2}, {x:81, y:17}, {x:85, y:35}, {x:87, y:49}, {x:88, y:56}, {x:89, y:62}, {x:88, y:71}, {x:86, y:82}, {x:86, y:97}, {x:86, y:110}, {x:93, y:114}, {x:105, y:117}, {x:124, y:116}, {x:138, y:116}, {x:154, y:119}, {x:172, y:122}, {x:188, y:121}, {x:198, y:118}, {x:209, y:113}, {x:211, y:104}, {x:205, y:101}, {x:197, y:101}, {x:185, y:99}, {x:175, y:98}, {x:166, y:91}, {x:154, y:76}, {x:146, y:66}, {x:139, y:56}, {x:133, y:32}, {x:129, y:15}, {x:126, y:-8}, {x:122, y:-39}, {x:120, y:-67}, {x:118, y:-96}, {x:115, y:-116}, {x:111, y:-141}, {x:91, y:-174}, {x:-5, y:-160}, {x:-70, y:-156}, {x:-75, y:-190} ] 

		@frames = ArrayUtils.shuffle(@frames)


	initBoids: (frameNum = 0) ->
		hw = @ctx.width * .5
		hh = @ctx.height * .5

		@boids = []
		frame = @frames[frameNum]
		for i in [0...frame.length]
			a = frame[i]
			# pos = new Vec3D(a.x * @scale + hw + @offsetX - 10, a.y * @scale + hh + @offsetY, 0)
			pos = new Vec3D(random(@ctx.width * .1, @ctx.width * .9), random(-50, 0), 0)
			color = '#ccc'
			b = new Boid(@ctx, pos, color)
			b.radius = random(1, 2)
			b.maxSpeed = random(0.2, 1)
			# b.stroke = true
			@boids.push(b)	